﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;

namespace AnalyzerGameHost
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    protected override void OnInitialized(EventArgs e)
    {
      var t = new Thread(() =>
        {
          Analyzer.Main.run(
            () => Dispatcher.BeginInvoke(new Action(this.ClearGraphs)),
            (name,width,height) => Dispatcher.BeginInvoke(new Action(() => this.AddGraph(name,width,height))),
            (name) => Dispatcher.BeginInvoke(new Action(() => this.RemoveGraph(name))),
            (name, caption, data) => Dispatcher.BeginInvoke(new Action(() => this.SetGraphData(name, caption, data))),
            () => Dispatcher.BeginInvoke(new Action(this.Close), null));
        });
      //t.IsBackground = true;
      t.Priority = ThreadPriority.AboveNormal;
      t.Start();

      //List<Chart> charts = new List<Chart>();
      ////foreach (var graph in graphs)
      //charts.Add(new Chart() { Title = "Prova", Margin = new Thickness(40), Width = 150, Height = 200 });

      //foreach (var chart in charts)
      //  this.Graphs.Children.Insert(0, chart);

      base.OnInitialized(e);
    }

    List<Chart> charts = new List<Chart>();

    void ClearGraphsSeries()
    {
      foreach (var chart in charts) chart.Series.Clear();
    }

    void AddGraph(string name, int width, int height)
    {
      var new_chart = new Chart() { Title = name, Name = name, Margin = new Thickness(40), Width = width, Height = height };
      charts.Add(new_chart);
      this.Graphs.Children.Insert(0, new_chart);

      this.UpdateLayout();
    }

    void RemoveGraph(string name)
    {
      var chart = charts.Find(c => c.Name == name);
      charts.Remove(chart);
      this.Graphs.Children.Remove(chart);
    }

    void SetGraphData(string name, string caption, IEnumerable<Tuple<string, double>> data)
    {
      var chart = charts.Find(c => c.Name == name);
      chart.Series.Clear();
      var graph_values = data.Select(x => new KeyValuePair<string, double>(x.Item1, x.Item2)).ToList();

      var new_series = new ColumnSeries();
      new_series.Title = caption;
      new_series.IndependentValueBinding = new Binding("Key");
      new_series.DependentValueBinding = new Binding("Value");
      new_series.ItemsSource = graph_values;
      //new_series.Background = Brushes.CornflowerBlue;
      //new_series.Foreground = Brushes.CornflowerBlue;
      //new_series.BorderBrush = Brushes.CornflowerBlue;
      new_series.DataPointStyle = this.Resources["DataPointBlueStyle"] as Style;
      chart.Series.Add(new_series);
    }

    private void ClearGraphs()
    {
      foreach (var chart in charts)
        this.Graphs.Children.Remove(chart);
      charts.Clear();
    }
  }
}
